package com.example.administrator.xxnapp.event;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.xxnapp.BaseActivity;
import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.EventBean;
import com.example.administrator.xxnapp.bean.UserBean;
import com.example.administrator.xxnapp.fragment.event.EventPresenter;
import com.example.administrator.xxnapp.fragment.event.IViewEvent;
import com.example.administrator.xxnapp.friend.FriendActivity;
import com.example.administrator.xxnapp.main.MainActivity;
import com.example.administrator.xxnapp.util.Config;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.mdruby.baselibrary.utils.ObjectStoreUtil;

public class EditEventActivity extends BaseActivity implements IViewEvent{
    private EventPresenter mPresenter;
    @BindView(R.id.act_edit_event_layout_TV_Friends)
    TextView mTVAddFriends;
    @BindView(R.id.act_edit_event_layout_ET_Describe)
    EditText mETDescribe;
    @BindView(R.id.act_edit_event_layout_ET_Content)
    EditText mETContent;
    @BindView(R.id.act_edit_event_layout_TV_Date)
    TextView mTVDate;
    private UserBean user;

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit_event;
    }

    @Override
    public void initBeforeDatas() {
        mPresenter = new EventPresenter(this,new EventBean());
        user = (UserBean) ObjectStoreUtil.getObject(mContext,Config.Key.USER_OBJECT_KEY);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void setListeners() {

    }

    @Override
    public void getEeventSuccess(List<EventBean> beans) {

    }

    @Override
    public void getEeventError(int resultCode, String message) {

    }

    @Override
    public void saveEventSuccess(String message) {
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setResult(RESULT_OK);
                EditEventActivity.this.finish();
            }
        },500);
    }

    @Override
    public void saveEventError(int resultCode, String message) {

    }

    @OnClick(R.id.act_edit_event_layout_TV_Submit)
    public void submit(View view){
        String describe = mETDescribe.getText().toString();
        String content = mETContent.getText().toString();
        String date = mTVDate.getText().toString();
        if (TextUtils.isEmpty(describe)){
            Toast.makeText(mContext, "请输入标题", Toast.LENGTH_SHORT).show();
        }
        mPresenter.saveEvent(user.getUserId(),date,content,describe);
    }

    @OnClick(R.id.act_edit_event_layout_TV_Friends)
    public void getFriends(View view){
        Intent intent = new Intent(mContext, FriendActivity.class);
        startActivityForResult(intent, Config.RequestCode.ADD_FRIENDS);
    }

    @OnClick(R.id.act_edit_event_layout_TV_Date)
    public void setDate(View view){
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog datePickerDialog = new DatePickerDialog(EditEventActivity.this, new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                String month = (monthOfYear+1)<10?("0"+(monthOfYear+1)):(""+(monthOfYear+1));
                mTVDate.setText(year + "-" + month + "-" + (dayOfMonth<10?("0"+dayOfMonth):(""+dayOfMonth)));
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            switch (requestCode){
                case Config.RequestCode.ADD_FRIENDS:
                    UserBean bean = (UserBean) data.getSerializableExtra(Config.ShareKey.FRIEND);
                    mTVAddFriends.setText(TextUtils.isEmpty(mTVAddFriends.getText().toString())?bean.getUserName():mTVAddFriends.getText()+","+bean.getUserName());
                    break;
            }
        }
    }
}
